//========encoding:utf-8=========
/*
 * @describe:  三目运算符
 * @author:mhao
 * @copyright:chinasoft.cn
 * @date:2020年4月1日   上午11:27:41
 */
package edu.chinasoft.handler;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import edu.chinasoft.domain.Application;
import edu.chinasoft.domain.Approval;
import edu.chinasoft.domain.Item;
import edu.chinasoft.domain.User;
import edu.chinasoft.service.ItemService;

@Controller("itemHandler")
@RequestMapping("/itemHandler")
public class ItemHandler {
	 private static Logger logger = LoggerFactory.getLogger(ItemHandler.class);
	 @Autowired
	 @Qualifier("itemService")
	 private ItemService itemService;
	//添加请假单明细
	    @RequestMapping(value = "/addApplicationItem.do",method = {RequestMethod.POST,RequestMethod.GET})
	    public String addApplicationItem(Integer applicationId,Integer approvalId, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	        logger.info(" ItemHandler >>> addApplicationItem start.......");
	        logger.info("当前申请单的id为："+applicationId+"，审批者的id为："+approvalId);
	        Application application = new Application();
	        application.setApplicationId(applicationId);
	        User user = new User();
	        user.setUserId(applicationId);
	        Approval approval = new Approval();
	        approval.setApprovalUser(user);
	        Item item = new Item();
	        item.setApplication(application);
	        item.setApproval(approval);
	        //获取当前时间
	        Date date = new Date();
	        //调用服务层方法
	        boolean result = itemService.insertItem(item);
	        String resultUrl="";
	        if(result) {
	        	resultUrl="redirect:/applicationHandler/showNoSubmitApplication.do";
	        }else {
	        	resultUrl="/additemerror";
	        }
	        logger.info(" ItemHandler >>> addApplicationItem end.......");
			return resultUrl;
	    }	        
	    @InitBinder
	    protected void initBinder(WebDataBinder binder) {
	        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	        dateFormat.setLenient(false);
	        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
	    }
}
